//
//  HLMatchNormalViewController.m
//  YueYue
//
//  Created by tiko on 2024/10/28.
//

#import "HLMatchNormalViewController.h"
#import "SVGA.h"
#import "HLMatchRequest.h"
#import "HLCallTool.h"

@interface HLMatchNewVideoModel : NSObject

@property (nonatomic, strong) NSString *age;
@property (nonatomic, strong) NSString *avatar;
@property (nonatomic, strong) NSString *birthday;
@property (nonatomic, strong) NSString *countryName;
@property (nonatomic, strong) NSString *nickName;
@property (nonatomic, strong) NSString *roomId;
@property (nonatomic, strong) NSString *userId;
@property (nonatomic, strong) NSString *videoPrice;

@end

@implementation HLMatchNewVideoModel

@end


@interface HLMatchNormalViewController ()
@property (nonatomic, strong) SVGAPlayer *videoPlayer;
@property (nonatomic, strong) SVGAParser *videoParser;
@property (nonatomic, strong) UIImageView *bgIV;
@property (nonatomic, strong) UIImageView *avatarIV;
@property (nonatomic, strong) UIButton *matchButton;

@end

@implementation HLMatchNormalViewController

- (void)setupNavigationItems
{
    [super setupNavigationItems];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.videoParser = [[SVGAParser alloc] init];//call_animation  vvTwentyAnimation
    [self.videoParser parseWithNamed:@"animation01" inBundle:nil completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
        self.videoPlayer.videoItem = videoItem;
        [self.videoPlayer startAnimation];
    } failureBlock:nil];
    
    [self.view addSubview:self.bgIV];
    [self.view addSubview:self.videoPlayer];
    [self.view addSubview:self.avatarIV];
    [self.view addSubview:self.matchButton];
    
    [self.view insertSubview:self.bgIV atIndex:0];
    
    [self.bgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.videoPlayer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(200);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(280, 280));
    }];
    [self.avatarIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.videoPlayer);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [self.matchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-100);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(200, 50));
    }];
}

- (void)matchButtonAction
{
    MJWeakSelf
    HLMatchRequest *request = [[HLMatchRequest alloc] initWithType:HLMatchTypeNewVideo params:@{}];
    [request startWithCompletionBlockWithSuccess:^(__kindof HLMatchRequest * _Nonnull request) {
        if (request.isSuccess) {
            
            HLMatchNewVideoModel *randomUser = [HLMatchNewVideoModel mj_objectWithKeyValues:[request.result valueForKey:@"data"]];
            HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
            user.userId = randomUser.userId;
            user.avatar = randomUser.avatar;
            user.nickName = randomUser.nickName;
            user.videoType = HLRtcCallVideoTypeNormal;
            [[HLCallTool shareInstance] matchUser:user OnVC:[QMUIHelper visibleViewController] callback:^(BOOL match) {
                if (match) {
//                    [weakSelf matchButtonAction];
                }
            }];
        } else {
            [QMUITips showWithText:request.message];
        }
    } failure:^(__kindof HLMatchRequest * _Nonnull request) {
        [QMUITips showWithText:request.message];
    }];
}

- (SVGAPlayer *)videoPlayer
{
    if (!_videoPlayer)
    {
        _videoPlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];
        _videoPlayer.loops = 0;
    }
    return _videoPlayer;
}

- (UIImageView *)avatarIV
{
    if (!_avatarIV) {
        _avatarIV = [[UIImageView alloc] init];
        [_avatarIV sd_setImageWithURL:[NSURL URLWithString:[HLRunTime shareInstance].mine.avatar]];
        _avatarIV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarIV.layer.cornerRadius = 30;
        _avatarIV.layer.masksToBounds = YES;
        _avatarIV.hidden = YES;
    }
    return _avatarIV;
}

- (UIImageView *)bgIV
{
    if (!_bgIV) {
        _bgIV = [[UIImageView alloc] initWithImage:UIImageMake(@"match_fate_background")];
        _bgIV.contentMode = UIViewContentModeScaleAspectFill;
        _bgIV.layer.masksToBounds = YES;
    }
    return _bgIV;
}

- (UIButton *)matchButton
{
    if (!_matchButton)
    {
        _matchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_matchButton setTitle:@"开始匹配" forState:UIControlStateNormal];
        [_matchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _matchButton.backgroundColor = HLMainColor;
        _matchButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _matchButton.layer.cornerRadius = 25;
        _matchButton.layer.masksToBounds = YES;
        [_matchButton addTarget:self action:@selector(matchButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _matchButton;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorClear];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorWhite;
}

- (nullable UIColor *)qmui_titleViewTintColor
{
    return UIColorWhite;
}

- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
@end
